package com.cloudcc.mobile.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.litesuits.android.log.Log;

/**
 * preference 文件操作工具类
 * 
 * @author wangzhiqiang
 * @date 2015-7-15 下午4:03:32
 * 
 */
public class PreferenceUtils {

	/**
	 * 移除某个字段
	 * 
	 * @param context
	 * @param name
	 * @param key
	 */
	public static void removeKey(Context context, String name, String key) {
		SharedPreferences sharedPreferences = getPreferences(context, name);
		Editor mEditor = sharedPreferences.edit();
		mEditor.remove(key);
		mEditor.commit();
	}

	/**
	 * 清空某个xml文件
	 * 
	 * @param context
	 * @param name
	 */
	public static void clear(Context context, String name) {
		Editor mEditor = getPreferences(context, name).edit();
		mEditor.clear();
		mEditor.commit();
	}

	/**
	 * 保存String
	 * 
	 * @param context
	 * @param name
	 * @param key
	 * @param value
	 */
	public static void commitString(Context context, String name, String key,
			String value) {
		Editor mEditor = getPreferences(context, name).edit();
		mEditor.putString(key, value);
		mEditor.commit();
	}

	/**
	 * 获得editor
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static Editor getEditor(Context context, String name) {
		return getPreferences(context, name).edit();
	}

	/**
	 * 取出String
	 * 
	 * @param context
	 * @param name
	 * @param key
	 * @param faillValue
	 * @return
	 */
	public static String getString(Context context, String name, String key,
			String faillValue) {
		return getPreferences(context, name).getString(key, faillValue);
	}

	/**
	 * 保存int
	 * 
	 * @param context
	 * @param name
	 *            xml文件名
	 * @param key
	 * @param value
	 */
	public static void commitInt(Context context, String name, String key,
			int value) {
		Editor mEditor = getPreferences(context, name).edit();
		mEditor.putInt(key, value);
		mEditor.commit();
	}

	/**
	 * 取出int 数据
	 * 
	 * @param context
	 * @param name
	 *            xml文件名
	 * @param key
	 * @param failValue
	 * @return
	 */
	public static int getInt(Context context, String name, String key,
			int failValue) {
		return getPreferences(context, name).getInt(key, failValue);
	}

	/**
	 * 保存 long 数据
	 * 
	 * @param context
	 * @param name
	 *            xml文件名
	 * @param key
	 * @param value
	 */
	public static void commitLong(Context context, String name, String key,
			long value) {
		Editor mEditor = getPreferences(context, name).edit();
		mEditor.putLong(key, value);
		mEditor.commit();
	}

	/**
	 * 取出 long
	 * 
	 * @param context
	 * @param name
	 *            xml文件名
	 * @param key
	 * @param failValue
	 * @return
	 */
	public static long getLong(Context context, String name, String key,
			long failValue) {
		return getPreferences(context, name).getLong(key, failValue);
	}

	/**
	 * 保存boolean
	 * 
	 * @param context
	 * @param name
	 *            xml文件名
	 * @param key
	 * @param value
	 */
	public static void commitBoolean(Context context, String name, String key,
			boolean value) {
		Editor mEditor = getPreferences(context, name).edit();
		mEditor.putBoolean(key, value);
		Log.d("issdkjfhsd","jajajaj"+":hahahah:"+ key+"::"+value);
		mEditor.commit();
	}

	/**
	 * 
	 * @param context
	 * @param name
	 * @param key
	 * @param failValue
	 * @return
	 */
	public static Boolean getBoolean(Context context, String name, String key,
			boolean failValue) {
		Log.d("issdkjfhsd","jajajaj"+":hahahah:"+ key+"::"+failValue);
		return getPreferences(context, name).getBoolean(key, failValue);
	}

	/**
	 * 获得 SharedPreferences 执行操作
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static SharedPreferences getPreferences(Context context, String name) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				name, Context.MODE_PRIVATE);
		return sharedPreferences;
	}
}
